#include "test_fs.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "cm_os.h"


static int test_fd;
void cm_test_fs_operation(unsigned char*cmd[],int len)
{
    if(0 == strncmp("OPEN",cmd[2],strlen("OPEN")))
    {
        const char *filename = cmd[4];
        cm_demo_printf("filename:%s\n",filename);
        if(0 == strcmp("CM_WB",cmd[3]))//w 打开只写文件，若文件存在则文件长度清为0，即该文件内容会消失。若文件不存在则建立该文件。
        {
            test_fd = cm_fs_open(filename, CM_FS_WB);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,test_fd,cm_fs_open);
        }else if(0 == strcmp("CM_RB",cmd[3]))//r 打开只读文件，该文件必须存在
        {
            test_fd = cm_fs_open(filename, CM_FS_RB);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,test_fd,cm_fs_open);
        }else if(0 == strcmp("CM_AB",cmd[3]))//a 以附加的方式打开只写文件。若文件不存在，则会建立该文件，如果文件存在，写入的数据会被加到文件尾，即文件原先的内容会被保留。
        {
            test_fd = cm_fs_open(filename, CM_FS_AB);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,test_fd,cm_fs_open);
        }else if(0 == strcmp("CM_WBPLUS",cmd[3]))//w+ 打开可读写文件，若文件存在则文件长度清为零，即该文件内容会消失。若文件不存在则建立该文件。
        {
            test_fd = cm_fs_open(filename, CM_FS_WBPLUS);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,test_fd,cm_fs_open);
        }else if(0 == strcmp("CM_ABPLUS",cmd[3]))//a+ 以附加方式打开可读写的文件。若文件不存在，则会建立该文件，如果文件存在，写入的数据会被加到文件尾后，即文件原先的内容会被保留。
        {
            test_fd = cm_fs_open(filename, CM_FS_ABPLUS);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,test_fd,cm_fs_open);
        }
        // else if(0 == strcmp("CM_RBPLUS",cmd[3]))
        // {
        //     test_fd = cm_fs_open(filename, CM_FS_RBPLUS);
        //     OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,test_fd,cm_fs_open);
        // }
        cm_demo_printf("cm_fs_open ret(fd): %d\n",test_fd);
    }else if(0 == strncmp("CLOSE",cmd[2],strlen("CLOSE")))
    {
        int32_t fd = atoi(cmd[3]);
        int close = cm_fs_close(fd);
	    cm_demo_printf("cm_fs_close ret:%d\n", close);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,close,cm_fs_close);
    }
    else if(0 == strncmp("READ",cmd[2],strlen("READ")))
    {
        int32_t fd = atoi(cmd[3]);
        uint32_t size = atoi(cmd[4]);
        char buf[1000] = {0};
        //cm_demo_printf("before read osKernelGetTickCount:%u\n",osKernelGetTickCount ());
        int32_t fs_read = cm_fs_read(fd, buf, size);
        cm_demo_printf("file read ret(len): %d\n",fs_read);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,fs_read,cm_fs_read);
        //cm_demo_printf("after read osKernelGetTickCount:%u\n",osKernelGetTickCount ());
        cm_demo_printf("file read : %s\n",buf);
    }else if(0 == strncmp("WRITE",cmd[2],strlen("WRITE")))
    {
        int32_t fd = atoi(cmd[3]);
        int32_t size = atoi(cmd[4]);
        char *buf = cmd[5];
        //cm_demo_printf("before write osKernelGetTickCount:%u\n",osKernelGetTickCount ());
        int32_t fs_write = cm_fs_write(fd, buf, size);
        cm_demo_printf("file write ret(len):%d\n",fs_write);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,fs_write,cm_fs_write);
        //cm_demo_printf("after write osKernelGetTickCount:%u\n",osKernelGetTickCount ());
    }
    else if(0 == strncmp("SYNC",cmd[2],strlen("SYNC")))
    {
        int32_t fd = atoi(cmd[3]);
	    cm_demo_printf("file sync ret:%d\n",cm_fs_sync(fd));
    }else if(0 == strncmp("SEEK",cmd[2],strlen("SEEK")))
    {
        int32_t fd = atoi(cmd[3]);
        int32_t offset = atoi(cmd[4]);
        int32_t base = atoi(cmd[5]);
        cm_demo_printf("fd:%d,offset:%d:base:%d\n",fd,offset,base);
        int32_t fs_seek = cm_fs_seek( fd,  offset,  base);
        cm_demo_printf("file seek ret:%d\n", fs_seek);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,fs_seek,cm_fs_seek);
        
        //cm_demo_printf("file seek ret:%d\n", cm_fs_seek( fd,  -10,  2));
    }
    else if(0 == strncmp("DEL",cmd[2],strlen("DEL")))
    {
        const char *file_name = cmd[3];
        int32_t fs_delete = cm_fs_delete(file_name);
	    cm_demo_printf("file delete ret:%d\n",fs_delete);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,fs_delete,cm_fs_delete);
    }
    else if(0 == strncmp("MOVE",cmd[2],strlen("MOVE")))
    {
        const char *src = cmd[3];
        const char *dest = cmd[4];
        int32_t fs_move = cm_fs_move(src, dest);
        cm_demo_printf("file move ret: %d\n",fs_move);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,fs_move,cm_fs_move);
    }
    else if(0 == strncmp("GET_SIZE",cmd[2],strlen("GET_SIZE")))
    {
        const char *file_name = cmd[3];
        int32_t fs_filesize = cm_fs_filesize(file_name);
	    cm_demo_printf("file size ret: %d\n",fs_filesize);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,fs_filesize,fs_filesize);
    }
    // else if(0 == strncmp("MK_DIR",cmd[2],strlen("MK_DIR")))
    // {
    //     const char *path = cmd[2];
	//     cm_demo_printf("make dir ret: %d\n",cm_fs_mkdir(path));
    // }
    // else if(0 == strncmp("RM_DIR",cmd[2],strlen("RM_DIR")))
    // {
    //     const char *path = cmd[2];
	//     cm_demo_printf("remove dir ret:%d\n", cm_fs_rmdir(path));
    // }
    else if(0 == strncmp("GET_INFO",cmd[2],strlen("GET_INFO")))
    {
        cm_fs_system_info_t info = {0};
        int32_t fs_getinfo = cm_fs_getinfo(&info);
        cm_demo_printf("file system info ret:%d\n",fs_getinfo );
        cm_demo_printf("file system total size:%d, remain size:%d\n",info.total_size,info.free_size);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,fs_getinfo,cm_fs_getinfo);
    }
    // else if(0 == strncmp("FIND_FIRST",cmd[2],strlen("FIND_FIRST")))
    // {
    //     const char *path = cmd[2];
    //     cm_fs_file_data_t file_data = {0};
    //     cm_demo_printf("file find first ret:%d\n",cm_fs_find_first(path, &file_data));
    // }
    // else if(0 == strncmp("FIND_NEXT",cmd[2],strlen("FIND_NEXT")))
    // {
    //     int32_t find_fd = atoi(cmd[2]);
    //     cm_fs_file_data_t file_data = {0};
    //     cm_demo_printf("file find next:%d\n", cm_fs_find_next( find_fd, &file_data));
    //     cm_demo_printf("file data attr:%d, size:%d, name:%s\n",file_data.file_attr,file_data.file_size,file_data.file_name);
    // }
    // else if(0 == strncmp("CLOSE_FIND",cmd[2],strlen("CLOSE_FIND")))
    // {
    //     int32_t find_fd = atoi(cmd[2]);
	//     cm_demo_printf("file find close ret:%d\n",cm_fs_find_close(find_fd));
    // }
    else if(0 == strncmp("EXIST",cmd[2],strlen("EXIST")))
    {
        const char *filename = cmd[3];
        cm_demo_printf("filename:%s\n",filename);
        int32_t fs_exit = cm_fs_exist(filename);
	    cm_demo_printf("cm_fs_exist ret:%d\n",fs_exit);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(1,fs_exit,cm_fs_exist);

        fs_exit = cm_fs_exist("not_exit_test1.txt");
	    cm_demo_printf("cm_fs_exist ret:%d\n",fs_exit);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,fs_exit,cm_fs_exist);
    }
    else{
        cm_demo_printf("param error!\n");
    }
}
